package com.iacrqq.sedaf.queue;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * @author sihai
 *
 */
public class QueueRuntimeStatisticsCollector<T>
{
	private volatile int size;						// 
	private volatile int capacity;					// 
	private volatile long full;						//	
	private volatile long empty;					// 
	
	private volatile long time;
	private volatile long total;
	
	private ConcurrentHashMap<T, Long> _store_;
	
	public QueueRuntimeStatisticsCollector()
	{
		_store_ = new ConcurrentHashMap<T, Long>();
	}
	
	public void setSize(int size)
	{
		this.size = size;
	}

	public void setCapacity(int capacity)
	{
		this.capacity = capacity;
	}
	
	public void enqueue(T element)
	{
		_store_.put(element, System.currentTimeMillis());
	}
	
	public void dequeue(T element)
	{
		time += (System.currentTimeMillis() - _store_.remove(element));
		total++;
	}
	
	public void enqueue(List<T> elementList)
	{
		long currentTime = System.currentTimeMillis();
		for(T element : elementList)
		{
			_store_.put(element, currentTime);
		}
	}
	
	public void dequeue(List<T> elementList)
	{
		
		long currentTime = System.currentTimeMillis();
		for(T element : elementList)
		{
			time += (currentTime - _store_.remove(element));
			total++;
		}
	}
	
	public void full()
	{
		this.full ++;
	}
	
	public void emtpy()
	{
		this.empty ++;
	}

	public QueueRuntimeStatistics snapshot()
	{
		QueueRuntimeStatistics statistics = new QueueRuntimeStatistics();
		
		statistics.setSize(size);
		statistics.setCapacity(capacity);
		statistics.setElementAverageWaitingTime(time / (total + 0.0D));
		return statistics;
	}
}
